using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.Samples.DirectX.UtilityToolkit;
using TTDotNet.Util;
using TTDotNet.Engine.Terrain;

using Microsoft.DirectX.Direct3D;

namespace TTDotNet.Engine.ThreeDEngine
{
    public class TexLoader
    {
        private Dictionary<TileType, GameTexture> textures;

        public TexLoader()
        {
            try
            {
                textures = new Dictionary<TileType, GameTexture>();
                //LoadGroundTextures();
                LoadRoadTextures();
                if (Game.debug) Game.logger.WriteLine("Texture Loader Initialization Completed Successfully after " + Game.texTime.ElapsedMilliseconds + "ms");
            }
            catch (Exception e)
            {
                Game.CloseOnException(e);
            }
        }

        public GameTexture GetTexture(TileType type)
        {
            return textures[type];
        }

        private void LoadGroundTextures()
        {
            Load(Utility.FindMediaFile("data/textures/ground_textures.dat"));
        }

        private void LoadRoadTextures()
        {
            Load(Utility.FindMediaFile("data/textures/road_textures.dat"));
        }

        private void Load(string path)
        {
            string str = null;

            List<string> dataStrings = new List<string>();

            try
            {
                if (File.Exists(path))
                {
                    FileStream texIn = new FileStream(path, FileMode.Open);
                    StreamReader sr = new StreamReader(texIn);

                    while ((str = sr.ReadLine()) != null)
                    {
                        dataStrings.Add(str);
                    }

                    sr.Close();
                    texIn.Close();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Error loading buttons file");
                }


            }
            catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); }

            CreateTextures(dataStrings);
        }

        private void CreateTextures(List<string> strings)
        {
            string[] delims = { "," };

            foreach (string s in strings)
            {
                try
                {
                    string[] data = s.Trim().Split(delims, StringSplitOptions.RemoveEmptyEntries);

                    if (data.Length != 2)
                    {
                        System.Windows.Forms.MessageBox.Show("Error loading vehicles\n Line" + s + " is malformed.");
                    }
                    else
                    {
                        try
                        {
                            
                            TileType type = (TileType)Enum.Parse(typeof(TileType), data[0]);
                            string texpath = data[1];

                            textures[type] = Game.textureCacher.LoadTexture(texpath);
                        }
                        catch (Exception e)
                        {
                            System.Windows.Forms.MessageBox.Show("Error parsing: " + e.Message);
                        }
                        
                    }
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("There has been an error loading the texture: " + s + "\n" + e.Message);
                }
            }
        }

    }
}
